<?php
/**
 * Date: 2018/6/6
 * Time: 10:48
 * Author: zeronlee
 */

namespace App\Services\Email;
use Illuminate\Support\Facades\Log;

class Email
{
    private $from;
    private $userName;
    private $userPwd;
    private $url;
    private $secret;
    private $acToken;
    private $method;
    private $format;
    private $key;

    public function __construct () {
        $config = config('email');
        $this->from     = $config['from'];
        $this->userName = $config['UserName'];
        $this->key      = $config['key'];
        $this->userPwd  = base64_encode(decrypt_func($config['UserPwd'], $this->key));
        $this->url      = $config['url'];
        $this->secret   = $config['secret'];
        $this->acToken  = $config['ac_token'];
        $this->method   = $config['method'];
        $this->format   = $config['format'];
    }


    /**
     * 组装请求参数数据
     * @param $user_parameters
     * @return array
     */
    private function getRequestParameters($user_parameters)
    {
        $KEY_FORMAT = "Format";
        $KEY_METHOD = "Method";
        $KEY_ACCESS_TOKEN = "AccessToken";
        $KEY_TS = "Timestamp";
        $KEY_PARAMETER = "Parameter";
        $KEY_SIGN = "Sign";

        //拼接参数数组
        $req_map[$KEY_METHOD] = $this->method;
        $req_map[$KEY_FORMAT] = $this->format;
        $req_map[$KEY_ACCESS_TOKEN] = $this->acToken;
        $date_str = date("Y-m-d H:i:s", time());
        $req_map[$KEY_TS] = $date_str;
        $req_map[$KEY_PARAMETER] = json_encode($user_parameters);

        //获取md5前的字符串
        $signString = $this->secret .
            $KEY_ACCESS_TOKEN . $this->acToken.
            $KEY_FORMAT . $this->format .
            $KEY_METHOD . $this->method .
            $KEY_PARAMETER . $req_map[$KEY_PARAMETER] .
            $KEY_TS . $req_map[$KEY_TS] .
            $this->secret;
        $sign = md5($signString);
        $req_map[$KEY_SIGN] = strtoupper($sign);

        return $req_map;
    }

    /**
     * 请求操作
     * @param $user_parameters
     * @return bool|string
     */
    public function odsPost( $user_parameters)
    {
        $data = http_build_query($this->getRequestParameters($user_parameters));
        $html = '';
        $opts = array(
            'http' => array(
                'method' => 'POST',
                'header' => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($data) . "\r\n",
                'content' => $data
            )
        );
        $context = stream_context_create($opts);
        $html = file_get_contents($this->url, false, $context);
        return $html;

    }

    /**
     * 发送
     * @param $userParameters
     * @return string
     */
    public function send($userParameters)
    {
        $userParameters["From"]     = $this->from;
        $userParameters["UserName"] = $this->userName;
        $userParameters["UserPwd"]  = $this->userPwd;
        $result = $this->odsPost( $userParameters);
        return $result;
    }

    private function odsGet($url, $user_parameters, $app_secret, $access_token, $method, $format)
    {
        $data = $this->getRequestParameters($user_parameters);

        $data = http_build_query($data);

        $html = file_get_contents($url . "?" . $data);

        return $html;
    }

    public function setMethod($method){
        $this->method = $method;
    }
}